// pages/main/addinvoice/addinvoice.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      invoiceType:[
        {name:'企业',value:0,dis:1},
        { name: '个人', value: 1,dis:0 }
      ],
      postData:{
        type:0
      },
      taxHidden:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  // 点击类型
  chooseType: function (e) {
    var val = e.currentTarget.dataset.val;
    var dis = e.currentTarget.dataset.dis;
    var addIconData = this.data.invoiceType;
    var postData = this.data.postData;
    postData.type = val;
    for (var i = 0; i < addIconData.length; i++) {
      addIconData[i].dis = 0;
    }
    addIconData[dis].dis = 1;
    var taxHidden = this.data.taxHidden;
    if (val == 0){
      taxHidden = 0
    }else{
      taxHidden = 1;
      postData.taxNum = ''
    }
    this.setData({
      invoiceType: addIconData,
      postData: postData,
      taxHidden: taxHidden
    })
  },
  //抬头
  inputName: function (e) {
    var val = e.detail.value;
    var postData = this.data.postData;
    postData.myName = val;
    this.setData({
      postData: postData
    })
  },
  //税号
  taxNum: function (e) {
    var val = e.detail.value;
    var postData = this.data.postData;
    postData.taxNum = val;
    this.setData({
      postData: postData
    })
  },
  //点击删除
  delInvoice: function (e) {
    wx.showModal({
      title: '删除发票',
      content: '确认是否要删除该发票吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 点击确定
  invoiceSave: function (e) {
    var postData = this.data.postData;
    var taxHidden = this.data.taxHidden;
    //判定抬头、税号不能为空
    if (postData.myName == '' || postData.myName == null) {
      wx.showToast({
        title: '请填写抬头',
        icon: 'none',
        duration: 1500
      })
    } else if (postData.taxNum == '' || postData.taxNum == null) {
      // 类型为个人时不需要填写税号
      if (taxHidden == 0){
        wx.showToast({
          title: '请填写税号',
          icon: 'none',
          duration: 1500
        })
      }else{
        console.log(postData)
      }
      
    } else {
      console.log(postData)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})